/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EntrySelector.java
 *
 * Created on 14-ago-2011, 2.04.19
 */
package agenda;

import agenda.database.DBManager;
import agenda.entityes.AgeEntry;
import java.awt.Color;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/**
 *
 * @author emanuele
 */
public class SearchDiag extends javax.swing.JDialog {

    AgeEntry entry = null;
    List<AgeEntry> list;

    /**
     * Creates new form EntrySelector
     */
    public SearchDiag(java.awt.Frame parent, boolean modal, List<AgeEntry> list) {
        super(parent, modal);
        initComponents();
        // imposta il colore di sfondo 145/210/235
        getContentPane().setBackground(new Color(145, 210, 235));

        if (list == null) {
            throw new IllegalArgumentException();
        }
        this.list = list;

        jTextField1.getDocument().addDocumentListener(new DocumentListener() {

            public void insertUpdate(DocumentEvent e) {
                search();
            }

            public void removeUpdate(DocumentEvent e) {
                search();
            }

            public void changedUpdate(DocumentEvent e) {
                search();
            }
        });

        // inizializza il combobox con i parametri per i quali è possibile effettuare la ricerca
        for (DBManager.SearchType x : DBManager.SearchType.values()) {
            srcTypeCB.addItem(x);
        }

        /*
         * imposta il listener per i cambiamentidel campo selectedIndex del
         * ComboBox"
         */
        srcTypeCB.addPropertyChangeListener("selectedIndex", new PropertyChangeListener() {

            public void propertyChange(PropertyChangeEvent pce) {
                search();
            }
        });

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jTextField1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        srcTypeCB = new javax.swing.JComboBox();
        linkCheck = new javax.swing.JCheckBox();
        jButton4 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        cancellB = new javax.swing.JButton();
        okB = new javax.swing.JButton();
        caseCK = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Ricerca voce rubrica"); // NOI18N
        setMinimumSize(new java.awt.Dimension(500, 300));
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jTextField1.setToolTipText("Inserisci il valore da ricercare");
        jTextField1.setName("jTextField1"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 260;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        getContentPane().add(jTextField1, gridBagConstraints);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jList1.setModel(new DefaultListModel());
        jList1.setName("jList1"); // NOI18N
        jScrollPane1.setViewportView(jList1);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.gridheight = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.2;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        getContentPane().add(jScrollPane1, gridBagConstraints);

        jLabel1.setName("jLabel1"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        getContentPane().add(jLabel1, gridBagConstraints);

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/edit_clear_locationbar_ltr.png"))); // NOI18N
        jButton3.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jButton3.setMinimumSize(new java.awt.Dimension(0, 0));
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                clearText(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        getContentPane().add(jButton3, gridBagConstraints);

        srcTypeCB.setToolTipText("Campo nel quale eseguire la ricerca"); // NOI18N
        srcTypeCB.setName("srcTypeCB"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipadx = 62;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        getContentPane().add(srcTypeCB, gridBagConstraints);

        linkCheck.setText("Mostra link"); // NOI18N
        linkCheck.setToolTipText("Fa in modo che nella lista dei risultati vengano inclusi anche le voci\nche hanno dei collegamenti con delle voci identificate dal testo inserito"); // NOI18N
        linkCheck.setName("linkCheck"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        getContentPane().add(linkCheck, gridBagConstraints);

        jButton4.setText("Dettagli"); // NOI18N
        jButton4.setName("jButton4"); // NOI18N
        jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                showDettails(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        getContentPane().add(jButton4, gridBagConstraints);

        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setLayout(new java.awt.GridBagLayout());

        cancellB.setText("Annulla"); // NOI18N
        cancellB.setName("cancellB"); // NOI18N
        cancellB.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cancellBMouseClicked(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        jPanel1.add(cancellB, gridBagConstraints);

        okB.setText("Ok"); // NOI18N
        okB.setName("okB"); // NOI18N
        okB.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                okPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        jPanel1.add(okB, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 0);
        getContentPane().add(jPanel1, gridBagConstraints);

        caseCK.setText("Case Sensitive"); // NOI18N
        caseCK.setToolTipText("Esegui la ricerca distinguandofra lettere maiuscole e minuscole");
        caseCK.setName("caseCK"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
        getContentPane().add(caseCK, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void cancellBMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancellBMouseClicked
    entry = null;
    dispose();
}//GEN-LAST:event_cancellBMouseClicked
    /*
     *
     */
    private void okPressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_okPressed
        // prendi l'oggetto selezionato (null se nn è stato selezionato nulla
        entry = (AgeEntry) jList1.getSelectedValue();
        dispose();
    }//GEN-LAST:event_okPressed

    /*
     * pulisce il campo ricerca
     */
    private void clearText(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_clearText
        jTextField1.setText("");
    }//GEN-LAST:event_clearText

    private void showDettails(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_showDettails
        int x = jList1.getSelectedIndex();
        if (x < 0) {
            return;
        }

        new InsertDiag(JOptionPane.getFrameForComponent(this),
                false,
                (AgeEntry) jList1.getModel().getElementAt(x),
                (List<AgeEntry>) null).setVisible(true);
    }//GEN-LAST:event_showDettails

    //TODO eliminare il contenuto della stringa se nel campo di ricerca non è inserito nulla
    // o fare in modo che vengano mostrate tutte le voci dell'agenda ?
    private void search() {
        String s = jTextField1.getText().trim();

        // prendi le voci che contengono s nel campo cognome
        List<AgeEntry> l = null;// DBManager.search(list, s);

        DBManager.SearchType type = (DBManager.SearchType) srcTypeCB.getSelectedItem();

        if (type == null) {
            return;
        }
        //esegui la ricerca
        l = DBManager.search(type, list, s, caseCK.isSelected(), linkCheck.isSelected());

        // elimina il contenuto della lista
        DefaultListModel model = (DefaultListModel) jList1.getModel();
        model.clear();

        for (AgeEntry e : l) {
            model.addElement(e);
        }
        System.out.println("Ricerca eseguita " + l.size());
        for (AgeEntry e : l) {
            System.out.println(e.toString());
        }
    }

    public AgeEntry getSelection() {
        setVisible(true);
        dispose();
        return entry;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancellB;
    private javax.swing.JCheckBox caseCK;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JCheckBox linkCheck;
    private javax.swing.JButton okB;
    private javax.swing.JComboBox srcTypeCB;
    // End of variables declaration//GEN-END:variables
}
